package com.cennavi.minemapdemo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.cennavi.minemapdemo.R;
import com.cennavi.minemapdemo.util.Config;
import com.cennavi.minemapdemo.util.DensityUtil;
import com.cennavi.minemapdemo.util.RandomUtil;
import com.minedata.minemap.MinemapAccountManager;
import com.minedata.minemap.camera.CameraPosition;
import com.minedata.minemap.map.MapView;
import com.minedata.minemap.map.MineMap;
import com.minemap.minemapsdk.maps.ImplMineMapOptions;


/**
 * Created by lijingru on 2018/11/21
 */
public class MapFragment extends Fragment {
    //地图视图
    private MapView mapView;
    //地图操作类
    private MineMap mineMap;
    private Context mContext;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.fragment_basemap, null);
        mContext = getActivity();
       MinemapAccountManager.getInstance(mContext, Config.MAP_TOK, Config.OFFLINE_TOK, Config.MAP_ID);
//        MinemapAccountManager.getInstance(mContext, "5d26fb730bdc48f3ad90aa4218646be6", "5d26fb730bdc48f3ad90aa4218646be6", "5699");
        ImplMineMapOptions mapOptions = ImplMineMapOptions.createFromAttributes(mContext,null);
        mapOptions.styleUrl(Config.mBaseUrl + Config.MAP_ID);

        mapOptions.logoEnabled(false);
        mapOptions.textureMode(true);

        mapView = new MapView(mContext,mapOptions);

        ConstraintLayout layout = view.findViewById(R.id.content);
        layout.addView(mapView,new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));


        initMap();

        return view;

    }

    private void initMap() {
//        mapView.setStyleUrl("http://dev.minedata.cn/service/solu/style/id/2229");
//        mapView.setStyleUrl("http://218.4.252.2:8800/kmap/service/solu/style/id/5699");
        mapView.addMapRenderCallback(new MapView.OnMapReadyListener() {
            @Override
            public void onMapReady(MineMap oMineMap) {
                mineMap = oMineMap;
                mineMap.getUiSettings().setZoomControlsEnabled(false);
                mineMap.getUiSettings().setRotateGesturesEnabled(true);// 是否可以旋转地图
                mineMap.getUiSettings().setTiltGesturesEnabled(true);

                //设置指南针是否显示，默认是显示
                mineMap.getUiSettings().setCompassEnabled(true);
                mineMap.getUiSettings().setCompassGravity(Gravity.LEFT);
//                mineMap.getUiSettings().setCompassImage(getResources().getDrawable(R.mipmap.icon_compass));
                mineMap.getUiSettings().setCompassMargins(DensityUtil.dip2px(mContext, 10), DensityUtil.dip2px(mContext, 10),
                        0, 0);
                mineMap.setCameraPosition(
                        new CameraPosition.Builder().target(RandomUtil.randomLatLon()).zoom(15)
                                .tilt(0)//设定倾斜角度
                                .bearing(0) //摄像机指向的方向,从北部顺时针方向设置
                                .build());
                mineMap.setMaxZoomPreference(17);//设置最高展示级别，最高为17级别
                mineMap.setMinZoomPreference(3);//设置最低的展示级别

            }
        });
    }
}
